//
//  QNLogger.h
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#define LOG_LEVEL_ERROR     100
#define LOG_LEVEL_WARNING   80
#define LOG_LEVEL_NOTICE    60
#define LOG_LEVEL_INFO      40
#define LOG_LEVEL_DEBUG     20
#define LOG_LEVEL_ALERT     10
#define LOG_LEVEL_VERBOSE   0

#ifdef __OPTIMIZE__
#define QNLogThreshold LOG_LEVEL_WARNING
#else
#define QNLogThreshold LOG_LEVEL_ALERT
#endif

#define _QNFile [[NSString stringWithUTF8String:__FILE__] lastPathComponent]
#define _QNLine __LINE__
#define _QNFunc __func__

#define QNControlledLog(level, ...)\
if (level >= QNLogThreshold) {NSLog(__VA_ARGS__);}

#define QNFormattedLog(level, tag, format, ...)\
QNControlledLog(level, @"%@ %s Line:%d\n%@: %@\n\n", _QNFile, _QNFunc,\
_QNLine, tag, [NSString stringWithFormat:(format), ##__VA_ARGS__])

#if QNLogThreshold <= LOG_LEVEL_ERROR
#define QNLogError(...) QNFormattedLog(LOG_LEVEL_ERROR, @"❌❌❌Error", __VA_ARGS__)
#else
#define QNLogError(...)
#endif

#if QNLogThreshold <= LOG_LEVEL_WARNING
#define QNLogWarning(...) QNFormattedLog(LOG_LEVEL_WARNING, @"⚠️⚠️⚠️Warning", __VA_ARGS__)
#else
#define QNLogWarning(...)
#endif

#if QNLogThreshold <= LOG_LEVEL_NOTICE
#define QNLogNotice(...) QNFormattedLog(LOG_LEVEL_NOTICE, @"📣📣📣Notice", __VA_ARGS__)
#else
#define QNLogNotice(...)
#endif

#if QNLogThreshold <= LOG_LEVEL_INFO
#define QNLogInfo(...) QNFormattedLog(LOG_LEVEL_INFO, @"✉️✉️✉️Info", __VA_ARGS__)
#else
#define QNLogInfo(...)
#endif

#if QNLogThreshold <= LOG_LEVEL_DEBUG
#define QNLogDebug(...) QNFormattedLog(LOG_LEVEL_DEBUG, @"🔥🔥🔥Debug", __VA_ARGS__)
#else
#define QNLogDebug(...)
#endif

#if QNLogThreshold <= LOG_LEVEL_ALERT
#define QNLogAlert(...) \
^(){\
    QNFormattedLog(LOG_LEVEL_ALERT, @"Alert", __VA_ARGS__);\
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"QNLogAlert" message:[NSString stringWithFormat:__VA_ARGS__] preferredStyle:UIAlertControllerStyleAlert];\
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleDestructive handler:nil];\
    [vc addAction:cancle];\
    [self presentViewController:vc animated:YES completion:nil];\
}()
#else
#define QNLogAlert(...)
#endif

#if QNLogThreshold <= LOG_LEVEL_VERBOSE
#define QNLogVerbose(...) QNFormattedLog(LOG_LEVEL_VERBOSE, @"Verbose", __VA_ARGS__)
#else
#define QNLogVerbose(...)
#endif
